package com.zx.idc.backend.gui.shiro;

import com.zx.idc.ds.sys.entity.SysUser;
import com.zx.idc.ds.sys.service.ISysMenuService;
import com.zx.idc.ds.sys.service.ISysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 * 系统安全认证实现类
 *
 * @auth jinyuanf
 * @date 2019/4/28 11:45
 **/
@Service
@DependsOn({"customCredentialsMatcher", "redisCacheManager"})
public class SysAuthorizingRealm extends AuthorizingRealm {


    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private CustomCredentialsMatcher customCredentialsMatcher;
    @Autowired
    private RedisCacheManager redisCacheManager;

    @PostConstruct
    public void initDependencies() {
        this.setCredentialsMatcher(customCredentialsMatcher);
        this.setCacheManager(redisCacheManager);
    }


    /**
     * 授权调用
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SysUser user = (SysUser) principals.fromRealm(getName()).iterator().next();
        if (user != null) {
            // 查询用户授权信息
            List<String> permissionsList = sysMenuService.findMenusPermissionsByUserId(user.getId());
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            for (String permissions : permissionsList) {
                String[] permissionArray = permissions.split(",");
                for (String permission : permissionArray) {
                    if (StringUtils.hasLength(permission)) {
                        info.addStringPermission(permission);
                    }
                }
            }
            info.addStringPermission("index:view");
            info.addStringPermission("sys:menus:view");
            return info;
        }

        return null;
    }


    /**
     * 用户登录调用
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        // 通过表单接收的用户名
        String username = token.getUsername();
        if (username != null && !"".equals(username)) {
            SysUser user = sysUserService.selectByLoginName(username);
            if (user != null) {
                return new SimpleAuthenticationInfo(
                    user, user.getPassword(), getName());
            }
        }
        return null;
    }


}
